import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;


public class Launcher {
	private static List<User> userList = new ArrayList<User>();
	public static boolean addUser(String name, int points, int type){
		Iterator<User> ite = userList.iterator();
		while(ite.hasNext()){
			User user = ite.next();
			if(name.equals(user.getName())){
				return false;
			}
		}
		userList.add(new User(name,points,type));
		return true;
	}
	
	public static void deleteByName(String name){
		Iterator<User> ite = userList.iterator();
		while(ite.hasNext()){
			User user = ite.next();
			if(name.equals(user.getName())){
				userList.remove(user);
				return;
			}
		}
	}
	
	public static User getUserByName(String name){
		Iterator<User> ite = userList.iterator();
		while(ite.hasNext()){
			User user = ite.next();
			if(name.equals(user.getName())){
				return user;
			}
		}
		return null;
	}
	
	public static void updatePointByName(String name, int points,int type){
		Iterator<User> ite = userList.iterator();
		while(ite.hasNext()){
			User user = ite.next();
			if(user.getName().equals(name)){
				user.setPoints(points);
				user.setRestPoints(points);
				user.setType(type);
			}
		}
	}
	
	public static String[] getUserNames(){
		String[] names = new String[userList.size()];
		for(int i = 0; i < userList.size(); i++){
			names[i] = userList.get(i).getName();
		}
		return names;
	}

	public static void main(String[] args){
		readUsers();
		showMainFrame();
	}
	
	public static void showMainFrame(){
		new MainFrame(userList);
	}

	public static void showBettingFrame(){
		String[] names = getUserNames();
		BettingFrame frame = new BettingFrame();
		frame.setNameList(names);
		frame.paintView();
		frame.setVisible(true);
	}

	public static void showDeleteUserFrame(){
		String[] names = getUserNames();
		DeleteUserFrame frame = new DeleteUserFrame();
		frame.setNameList(names);
		frame.setVisible(true);
	}
	
	public static void showModifyFrame(){
		String[] names = getUserNames();
		ModifyPointFrame frame = new ModifyPointFrame();
		frame.setNameList(names);
		frame.initialType();
		frame.setVisible(true);
	}
	
	public static void getBettingResult(int result){
		Iterator<User> ite = userList.iterator();
		while(ite.hasNext()){
			User user = ite.next();
			int subNum = 0;
			subNum += user.getSinglePoints();
			subNum += user.getDoublePoints();
			subNum += user.getSmallPoints();
			subNum += user.getBigPoints();
			subNum += user.getSmallSinglePoints();
			subNum += user.getSmallDoublePoints();
			subNum += user.getBigSinglePoints();
			subNum += user.getBigDoublePoints();
			for(int i = 0; i < User.NUM; i++){
				subNum += user.getEachPoints()[i];
			}
			user.setSubNum(subNum);
			int addNum = 0;
			if(result % 2 == 1){
				addNum += (user.getSinglePoints() * 2);
			}else{
				addNum += (user.getDoublePoints() * 2);
			}
			if(result < 14){
				addNum += (user.getSmallPoints() * 2);
			}else{
				addNum += (user.getBigPoints());
			}
			if(result % 2 == 1 && result > 14){
				addNum += (user.getBigSinglePoints() * 4);
			}
			if(result % 2 == 0 && result > 14){
				addNum += (user.getBigDoublePoints() * 4);
			}
			if(result % 2 == 1 && result < 13){
				addNum += (user.getSmallSinglePoints() * 4);
			}
			if(result % 2 == 0 && result < 13){
				addNum += (user.getSmallDoublePoints() *4);
			}
			addNum += (user.getEachPoints()[result] * 10);
			user.setAddNum(addNum);
			user.addRestPoints(addNum);
			user.totalInitialValue();
			user.setPoints(user.getRestPoints());
		}
	}
	
	public static void readUsers(){
		File file = new File("users.dat");
		if(! file.exists()){
			return;
		}
		Scanner input;
		try {
			input = new Scanner(file);
			while(input.hasNext()){
				String name = input.next();
				int points = input.nextInt();
				int type = input.nextInt();
				userList.add(new User(name,points,type));
			}
			input.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void writeUsers(){
		File file = new File("users.dat");
		try {
			PrintWriter output = new PrintWriter(file);
			Iterator<User> ite = userList.iterator();
			while(ite.hasNext()){
				User user = ite.next();
				output.print(user.getName() + " ");
				output.print(user.getPoints() + " ");
				output.println(user.getType());
			}
			output.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
